<template>
  <el-dialog v-model="dialogVisible" width="540" title="确认授权" draggable destroy-on-close :close-on-click-modal="false"
    custom-class="login-form-dialog">
    <el-form ref="formRef" :model="formData" :rules="rules" label-position="right" label-width="150px">
      <el-form-item label="所属企业：">
        {{formData.enterName}}
      </el-form-item>
      <el-form-item label="账&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号：">
        {{formData.username}}
      </el-form-item>
      <div class="tip-box">
        <img src="@/assets/imgs/tip-icon.png" mode="scaleToFill" style="width: 20px;height: 20px;" />
        跳过后无法查看该账号下的业务数据，需要您去手动授权，请您谨慎操作
      </div>
    </el-form>
    <template #footer>
      <div class="footer">
        <el-button @click="cancelAuth">跳过</el-button>
        <el-button type="primary" @click="submitForm">确认授权</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
  import { cloneDeep } from 'lodash'
  import * as LoginApi from '@/api/login'
  import * as authorizationManageApi from '@/api/authorizationManage'

  const emit = defineEmits(['success'])
  const message = useMessage()
  const validMess = ref('获取验证码')
  const formLoading = ref(false)
  const dialogVisible = ref(false)
  const formRef = ref()
  const formData = ref({
    enterName: undefined,
    username: undefined
  })
  const authForm = ref({
    username: undefined,
    password: undefined
  })

  const rules = reactive({
    enterpriseName: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
  })

  const open = async (params) => {
    dialogVisible.value = true
    resetFn()
    getBasicUserInfoById(params)
  }

  const getBasicUserInfoById = async (params) => {
    const res = await LoginApi.getBasicUserInfoById(params)
    formData.value = {
      enterName: res.enterName,
      username: res.username
    }
    authForm.value = {
      username: res.username,
      password: res.password
    }
  }

  const cancelAuth = () => {
    emit('success', false)
  }

  const submitForm = async () => {
    await authorizationManageApi.accountAuthorize(authForm.value)
    message.success('授权成功')
    emit('success', true)
    dialogVisible.value = false
  }

  const resetFn = () => {
    formData.value = {
      enterName: undefined,
      username: undefined
    }
    authForm.value = {
      username: undefined,
      password: undefined
    }
    formRef.value?.resetFields()
  }

  defineExpose({ open })
</script>

<style lang="scss" scoped>
  .el-button.code {
    color: #409EFE !important;
  }

  .tip-box {
    display: flex;
    padding: 8px 10px;
    color: #e6a23c;
    border-radius: 4px;
    font-size: 14px;
    background: rgba(230, 162, 60, 0.10);

    img {
      margin-right: 2px;
    }
  }

  :deep(.el-input--default) {
    height: 40px;
    line-height: 40px;
    font-size: 16px;
  }

  :deep(.el-button--large) {
    font-size: 16px;
  }

  :deep(.el-input__password) {
    font-size: 18px;
  }

  :deep(.el-form-item__label) {
    font-size: 16px;
    color: #909399;
  }

  :deep(.el-form-item__content) {
    font-size: 16px;
    color: #303133;
  }
</style>

<style lang="scss">
  .el-dialog.login-form-dialog {
    background: linear-gradient(181deg, #cae4ff 0%, #ffffff 24%);
    box-shadow: 0px 12px 48px 0px rgba(0, 0, 0, 0.03), 0px 9px 28px 0px rgba(0, 0, 0, 0.05), 0px 6px 16px 0px rgba(0, 0, 0, 0.08);
  }

  .login-form-dialog .el-dialog__header {
    padding: 30px 40px;
  }

  .login-form-dialog .el-dialog__title {
    font-weight: 500;
    font-size: 28px;
    font-family: PingFang SC, PingFang SC-500;
  }

  .login-form-dialog .el-dialog__body {
    padding: 0px 40px !important;
  }

  .login-form-dialog .el-dialog__footer {
    margin-top: 50px;
    padding: 10px 20px;
    border-top: 1px solid #E4E7ED;
  }
</style>